//
//  ProgerssView.m
//  Estate
//
//  Created by heyong on 2016/11/9.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "ProgerssView.h"

@implementation ProgerssView

- (UIProgressView *)progress{
    if(_progress==nil){
        _progress=[[UIProgressView alloc]init];
        
    }
    return _progress;
}

- (UILabel *)lab{
    if(_lab==nil){
        _lab=[[UILabel alloc]init];
    }
    return _lab;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        [self createView];
    }
    return self;
}

- (void)createView{
    
    self.backgroundColor=[[UIColor blackColor]colorWithAlphaComponent:0.25];
    //自定义视图
    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300*BIZ, 180*BIZ)];
    backView.backgroundColor=[UIColor whiteColor];
    backView.layer.cornerRadius=8*BIZ;
    
    //进度条
    i=0.1;
    timer =  [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];

    
    //进度条
    self.progress.backgroundColor=[UIColor colorWithRed:0.15 green:0.72 blue:0.90 alpha:1.00];
    self.progress.trackTintColor=[UIColor colorWithRed:0.66 green:0.66 blue:0.66 alpha:1.00];
    [backView addSubview:self.progress];
    [self.progress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(backView.mas_centerX);
        make.centerY.mas_equalTo(backView.mas_centerY);
        make.width.mas_equalTo(backView.mas_width).multipliedBy(0.9);
        make.height.offset(2.5*BIZ);
    }];
    
    //提示文字
    UILabel *title=[[UILabel alloc]init];
    title.textColor=[UIColor colorWithRed:0.29 green:0.71 blue:0.87 alpha:1.00];
    title.font=[UIFont systemFontOfSize:18*BIZ];
    [backView addSubview:title];
    title.textAlignment=1;
    title.text=@"数据同步中...";
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_equalTo(backView.mas_height).multipliedBy(0.25);
    }];
    
    //分割线
    UIView *line1=[[UIView alloc]init];
    line1.backgroundColor=[UIColor colorWithRed:0.15 green:0.72 blue:0.89 alpha:1.00];
    [backView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(title.mas_bottom).offset(1);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.mas_offset(1);
    }];
    
    //进度显示
    [backView addSubview:self.lab];
    self.lab.textColor=[UIColor colorWithRed:0.29 green:0.71 blue:0.87 alpha:1.00];
    self.lab.font=[UIFont systemFontOfSize:14*BIZ];
    [self.lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_progress.mas_left);
        make.top.mas_equalTo(_progress.mas_bottom).mas_offset(3);
        make.width.offset(40*BIZ);
        make.height.offset(20*BIZ);
    }];
    
    //分割线
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=[UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.00];
    [backView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.mas_offset(1);
        make.centerY.multipliedBy(1.5);
    }];
    
    //退出按钮
    UIButton *btn=[[UIButton alloc]init];
    [btn setTitle:@"退  出" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.top.mas_equalTo(line.mas_bottom).offset(0);
        make.bottom.mas_offset(0);
    }];
    [btn addTarget:self action:@selector(cancelDownLoad) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:backView];
    backView.center=self.center;
    
}

#pragma mark -进度条
- (void)timeFireMethod{
    i=i+0.005;;
//    NSLog(@"%f",i);
    dispatch_async(dispatch_get_main_queue(), ^{
        self.lab.text=[NSString stringWithFormat:@"%.f%%",i*100];
        self.progress.progress=i;
    });
    
    if(i>=1.0){
//        [timer invalidate];
        //同步完成
        [self cancelDownLoad];
    }
}

- (void)cancelDownLoad{
    [timer setFireDate:[NSDate distantFuture]];
    [self removeFromSuperview];
//    TimeingSynchronization *aaa=[[TimeingSynchronization alloc]init];
//    [aaa isSyncLocalData];

    NSDictionary *dic=@{@"isHiden":@"1"};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"indexReloadBtn" object:nil userInfo:dic];
    // 通知首页刷新数据
    [[NSNotificationCenter defaultCenter] postNotificationName:@"home" object:nil];
    NSLog(@"同步完成");
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
